package com.leetcode.LC;

import java.util.ArrayList;
import java.util.List;

public class LC100 {

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null || q == null) {
            return false;
        }

        List<Integer> NumListP = new ArrayList<Integer>();
        List<Integer> NumListQ = new ArrayList<Integer>();
        qinaxuDigui(p, NumListP);
        qinaxuDigui(q, NumListQ);
        return NumListP.equals(NumListQ);
    }

    // 用递归的方法进行先序遍历
    public void qinaxuDigui(TreeNode treeNode, List<Integer> qianxuNumList) {
        qianxuNumList.add(treeNode.val);
        if (treeNode.left != null) {
            qinaxuDigui(treeNode.left, qianxuNumList);
        } else {
            qianxuNumList.add(-1);
        }
        if (treeNode.right != null) {
            qinaxuDigui(treeNode.right, qianxuNumList);
        } else {
            qianxuNumList.add(-1);
        }
    }

    public static void main(String[] args) {

    }

    public boolean isSameTree_2(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null || q == null) {
            return false;
        }
        return p.val == q.val && (isSameTree_2(p.left, q.left) && isSameTree_2(p.right, q.right));
    }

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
